﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using WebSite.NewsHandle;
public partial class WebSite_Default : System.Web.UI.Page 
{
    string importantContent;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                
                NewsHandler newsHandler = new NewsHandler("Data Source=Localhost;Integrated Security=SSPI;Initial Catalog=CSHDB");
                //重要新闻标题
                NewsDataList.DataSource = newsHandler.GetNewBySection(0,5,1);
                NewsDataList.DataBind();
                //健康信息标题
                HealthDataList.DataSource = newsHandler.GetNewBySection(2,5,1);
                HealthDataList.DataBind();
                //滚动重要通知
                DataSet ds = newsHandler.GetNewBySection(1, 5,1);
                importantContent = "<Marquee OnMouseOver='this.stop();' OnMouseOut='this.start();' direction='up' scrollamount='2' >";
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    importantContent += "<a href='#' OnClick=" + "javascript:window.open('news.aspx?Id=" + ds.Tables[0].Rows[i]["Id"].ToString().Trim()
                                        + "','news'); style=\"text-decoration:none;\"" + ">" + "<font size='3px' color='#000000'>" + ds.Tables[0].Rows[i]["Title"].ToString() + "</font><br>" + ds.Tables[0].Rows[i]["Content"].ToString().Substring(0, 130) + "...." + "<br><br>";
                }
                importantContent += "</Marquee>";
                ImportantInfoLabel.Text = importantContent;
            }
            return;
         }       
        catch(Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    /// <summary>
    /// 登录模块   杨义根
    /// </summary>
    /// <param name="starttime"></param>
    /// <param name="endtime"></param>
    /// <returns></returns>
    string ShowDuration(object starttime, object endtime)
    {
        System.DateTime starttimeDT = (DateTime)starttime;
        if (endtime != null && endtime != DBNull.Value)
        {
            System.DateTime endtimeDT = (DateTime)endtime;
            if (starttimeDT.Date == endtimeDT.Date)
            {
                if (starttimeDT == endtimeDT)
                {
                    return starttimeDT.ToString("h:mm tt");
                }
                else
                {
                    return starttimeDT.ToString("h:mm tt") + " - " + endtimeDT.ToString("h:mm tt");
                }
            }
            else
            {
                return "thru " + endtimeDT.ToString("d");
            }
        }
        else
        {
            return starttimeDT.ToString("h:mm tt");
        }
    }

    void logout_click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
    }
    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        try
        {
            string role = "";
            bool Authenticated = false;
            //Authenticated = SiteLevelCustomAuthenticationMethod(Login1.UserName, Login1.Password);
            //注意，由于不同机子数据库存放位置可能不同，叫法不同，所以server=win-oh4ekl5tp66可能会不同机子不同，比如杨义根原来是server=NANKAI-8434F457\\SQLEXPRESS;现在的是我机子的改动。
            System.Data.SqlClient.SqlConnection nwindConn = new System.Data.SqlClient.SqlConnection("server=localhost;Integrated Security=SSPI;Initial Catalog=CSHDB");
            System.Data.SqlClient.SqlCommand catCMD = nwindConn.CreateCommand();
            catCMD.CommandText = " SELECT UserId,CountNO,UserIdentity FROM UserInfo where UserId='" + Login1.UserName + "' and " + "PASSWORD='" + Login1.Password + "'";
            nwindConn.Open();
            System.Data.SqlClient.SqlDataReader Dr = catCMD.ExecuteReader();
            int count = 0;
            if (Dr.Read())
            {
                Session.Timeout = 120;
                count = Dr.GetInt32(1);
                Authenticated = true;
                role = Dr["UserIdentity"].ToString();
                Response.Write(role);
                string username1 = Dr["UserId"].ToString().Trim();

                Session["UserId"] = username1;
                Session["UserIdentity"] = role;
                Dr.Close();

                count++;
                catCMD.CommandText = "UPDATE UserInfo set CountNO='" + count.ToString() + "' where UserId='" + Login1.UserName + "'";
                catCMD.ExecuteReader();

            }
            nwindConn.Close();
            /*if (Authenticated)
            {
                e.Authenticated = true;
            }
            else e.Authenticated = false;*/

            if (Authenticated == true)  //根据不同权限进入不同页面。0为学生，1为教师，2为管理员
            {
                if (role == "0")
                {
                    HttpContext.Current.Response.Redirect("check.aspx", false);
                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                    return;
                }
                else if (role == "1")
                {
                    Response.Redirect("t_data_contrast.aspx",false);
                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                    return;
                }
                else if (role == "2")
                {
                    Response.Redirect("m_news_check.aspx", false);
                   HttpContext.Current.ApplicationInstance.CompleteRequest();
                   return;
                }
            }
            return;
        }
        catch(Exception ex)
        {
            Response.Redirect("Default.aspx",false);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            return;
        }
    }
   
   
}
